import axios from 'axios'
import { Indicator, Toast } from 'mint-ui'
import router from '@/router'
import {
  setSessionStorage,
  getSessionStorage,
  removeSessionStorage
} from '@/utils/mixin'
class Http {
  constructor() {
    this.Domain = 'http://test.sportjx.com'
  }
  require(options) {
    if (!options.api) throw new Error('api 不能为空')
    if (!options.param) {
      options.param = {}
    }
    if (!options.methods) {
      options.methods = 'POST'
    } // 不传递方法默认为POST
    if (!options.loadingVisble) {
      options.loadingVisble = true
    } // 不传递默认开启loading
    if (!options.loadingText) {
      options.loadingText = '加载中...'
    }
    if (options.loadingVisble) {
      Indicator.open({
        text: options.loadingText,
        spinnerType: 'snake'
      })
    }
    return new Promise((resolve, reject) => {
      return axios({
        method: options.methods,
        url: options.api,
        baseURL: 'http://www.sportjx.com',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        data: options.param
      }).then(
        response => {
          Indicator.close()
          if (response.data.status === 200) {
            // 请求成功
            return resolve(response.data)
          } else {
            return reject(response.data)
          }
        },
        error => {
          Indicator.close()
          return reject(error)
        }
      )
    })
  }
}

export default Http
